package controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.AutomaticObjective;
import models.Dieta;
import models.Paciente;
import models.Plan;
import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;
import controllers.securesocial.SecureSocial;

@With(SecureSocial.class)
public class AutomaticObjectives extends CustomCRUD {

	public static void list(int page, String search, String searchFields, String orderBy, String order) {

		Paciente paciente = (Paciente)getUsuarioLogueado();

		Long userId = paciente.id;

		ObjectType type = ObjectType.get(getControllerClass());
		notFoundIfNull(type);

		if (page < 1) {
			page = 1;
		}

		List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));

		List<Model> filteredObjects = new ArrayList<Model>();

		for (Model model : objects) {
			if (((AutomaticObjective) model).getPaciente().id.equals(userId)) {
				filteredObjects.add(model);
			}
		}

		objects = filteredObjects;

		Long count = new Long(objects.size());
		Long totalCount = new Long(objects.size());

		try {
			render(type, objects, count, totalCount, page, orderBy, order, userId);
		} catch (TemplateNotFoundException e) {
			render(getControllerName() + "/list.html", type, objects, count, totalCount, page, orderBy, order, userId);
		}
	}
	
	public static void newObjetivoAutomatico(Long userId) throws Exception {

		ObjectType type = ObjectType.get(getControllerClass());

		notFoundIfNull(type);
		
		AutomaticObjective object = new AutomaticObjective();

		render(getControllerName() + "/newAutomaticObjective.html", type, userId, object);
	}
	
	public static void addObjetivoAutomatico(Long userId, String nombre, String fechaLimiteStr) throws Exception {

		AutomaticObjective o = new AutomaticObjective();

		Paciente paciente = (Paciente) getUsuarioLogueado();
		o.setPaciente(paciente);

		o.setNombreAutomaticObjective(nombre);

		o.setCumplidoAutomaticObjective(false);
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		
		java.util.Date fechaLimite = sdf.parse(fechaLimiteStr);
		o.setFechaLimiteAutomaticObjective(fechaLimite);

		o.save();

		list(0, null, null, null, null);
	}
	
	public static void show(String id) throws Exception {

		ObjectType type = ObjectType.get(getControllerClass());
		notFoundIfNull(type);

		AutomaticObjective object = (AutomaticObjective) type.findById(id);

		notFoundIfNull(object);

		try {
			render(type, object);
		} catch (TemplateNotFoundException e) {
			render(getControllerName() + "/show.html", type, object);
		}
	}
	
	public static List<List<Long>> cargarGrafico(Date fechaInicio, Date fechaFin, Long objetivoId) throws Exception {

		List<List<Long>> datosGrafico = new ArrayList<List<Long>>();

		ObjectType type = ObjectType.get(getControllerClass());
		notFoundIfNull(type);

		AutomaticObjective objetivo = (AutomaticObjective) type.findById(objetivoId.toString());

		notFoundIfNull(objetivo);
		
		Plan planActivo = objetivo.getPaciente().buscarPlanActivo();
		
		Long datoIndex = 0L;
		
		// arma una cadena asi [1,1890],[2,2004],[3,2590],[4,1989]
		for (Dieta dieta : planActivo.getDietas()) {
			
			List<Long> registro = new ArrayList<Long>();
			
			if(dieta.getFecha().after(fechaInicio) && dieta.getFecha().before(fechaFin) && dieta.calcularCaloriasConsumidas() > 0) {
				registro.add(datoIndex++);
				registro.add(new Long(dieta.calcularCaloriasConsumidas()));
				datosGrafico.add(registro);
			}
		}
		
		return datosGrafico; 
		
	}
	
}
